package common

import (
	"strconv"
	"strings"
)

// string 转化为 int64
func StringToInt64(string string) (res int64, err error) {
	res, err = strconv.ParseInt(string, 10, 64)
	return
}

// int64 转化为 string
func Int64ToString(int int64) (res string) {
	res = strconv.FormatInt(int, 10)
	return
}

func StringToFloat64(string string) (res float64, err error) {
	res, err = strconv.ParseFloat(string, 64)
	return
}

// CompareGoVersions 比较两个Go语言版本号的大小
func CompareGoVersions(v1, v2 string) int {
	var res int
	s1 := strings.Split(v1, ".")
	s2 := strings.Split(v2, ".")
	ver1Len := len(s1)
	ver2Len := len(s2)
	verLen := ver1Len
	if len(s1) < len(s2) {
		verLen = ver2Len
	}
	for i := 0; i < verLen; i++ {
		var ver1Int, ver2Int int
		if i < ver1Len {
			ver1Int, _ = strconv.Atoi(s1[i])
		}
		if i < ver2Len {
			ver2Int, _ = strconv.Atoi(s2[i])
		}

		if ver1Int < ver2Int {
			res = -1
			break
		}
		if ver1Int > ver2Int {
			res = 1
			break
		}
	}

	return res
}
